/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 14 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.platform.betfair;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.*;

/**
 * This class is responsible for decoding a supplied XML message,
 * in text form, into an object representation.
 *
 */
public class MessageDecoder {

	/**
	 * This method decodes the supplied XML text based stream, to
	 * return an object based representation of the message content.
	 * 
	 * @param is The input stream containing the message content
	 * @return The object representation of the message
	 * @throws Exception Failed to decode message
	 */
	public static Object getMessage(java.io.InputStream is) throws Exception {
		Object ret=null;
		
		ret = m_xstream.fromXML(is);
		
		is.close();
		
		return(ret);
	}
	
	private static XStream m_xstream=null;
	
	static {
		m_xstream = new XStream(new DomDriver());

		// soap:Envelope
		m_xstream.alias("soap:Envelope", SOAPEnvelope.class);
		m_xstream.aliasField("soap:Body", SOAPEnvelope.class, "body");
		
		// soap:Body
		m_xstream.alias("soap:Body", SOAPBody.class);
		m_xstream.aliasField("n:loginResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:logoutResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:getAccountFundsResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:getActiveEventTypesResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:getAllMarketsResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:getMarketResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:getCompleteMarketPricesCompressedResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:getMUBetsResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:placeBetsResponse", SOAPBody.class, "response");
		m_xstream.aliasField("n:cancelBetsResponse", SOAPBody.class, "response");
		
		// n:loginResponse
		m_xstream.alias("n:loginResponse", Response.class);
		m_xstream.aliasField("n:Result", Response.class, "result");

		// n:logoutResponse
		m_xstream.alias("n:logoutResponse", Response.class);

		// n:getAccountFundsResponse
		m_xstream.alias("n:getAccountFundsResponse", Response.class);

		// n:getActiveEventTypesResponse
		m_xstream.alias("n:getActiveEventTypesResponse", Response.class);

		// n:getAllMarketsResponse
		m_xstream.alias("n:getAllMarketsResponse", Response.class);

		// n:getMarketResponse
		m_xstream.alias("n:getMarketResponse", Response.class);

		// n:getCompleteMarketPricesCompressedResponse
		m_xstream.alias("n:getCompleteMarketPricesCompressedResponse", Response.class);

		// n:getMUBetsResponse
		m_xstream.alias("n:getMUBetsResponse", Response.class);

		// n:placeBetsResponse
		m_xstream.alias("n:placeBetsResponse", Response.class);

		// n:cancelBetsResponse
		m_xstream.alias("n:cancelBetsResponse", Response.class);

		// n2:EventType
		m_xstream.alias("n2:EventType", EventTypeItem.class);
		
		// n2:EventId
		m_xstream.alias("n2:EventId", Integer.class);
		
		// n2:MUBet
		m_xstream.alias("n2:MUBet", BetItem.class);
		
		// n2:PlaceBetsResult
		m_xstream.alias("n2:PlaceBetsResult", BetResultItem.class);
		
		// n2:CancelBetsResult
		m_xstream.alias("n2:CancelBetsResult", BetResultItem.class);
		
		// n2:Runner
		m_xstream.alias("n2:Runner", RunnerItem.class);
		
		// n2:EventType
		m_xstream.alias("market", MarketItem.class);
		
		// n:Result
		m_xstream.alias("n:Result", Result.class);
		
		// header
		m_xstream.alias("header", Header.class);
	}
}
